package com.droowa.model.dao;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.droowa.domain.BoardClass;
import com.droowa.domain.BoardList;
import com.droowa.domain.BoardManagement;
import com.droowa.domain.BoardOrder;
import com.droowa.domain.BoardSort;

@Repository
public class BoardManagementDAOImpl implements BoardManagementDAO {
	
	@Autowired
	private SqlSessionTemplate session;
	
	@Override
	public List<BoardClass> searchBoardClass() throws SQLException {
		return session.selectList(makeSqlId("searchBoardClass"));
	}

	@Override
	public List<BoardManagement> searchBoardManagement() throws SQLException {
		return session.selectList(makeSqlId("searchBoardManagement"));
	}
	
	@Override
	public BoardManagement searchBoardManagementId(int boardId) throws SQLException {
		// TODO Auto-generated method stub
		return session.selectOne(makeSqlId("searchBoardManagementId"), boardId);
	}

	@Override
	public int insertBoardManagement(BoardManagement bm) throws SQLException {
		return session.insert(makeSqlId("insertBoardManagement"), bm);
	}

	@Override
	public int createBoardPaper(int boardId) throws SQLException {
		return session.update(makeSqlId("createBoardPaper"), makeTableMap(makeTableName(boardId)));
	}

	@Override
	public int createBoardPaperSequence(int boardId) throws SQLException {
		return session.update(makeSqlId("createBoardPaperSequence"), makeTableMap(makeSequenceName(boardId)));
	}

	@Override
	public int insertBoardOrder(BoardOrder bo) throws SQLException {
		return session.insert("insertBoardOrder", bo);
	}
	
	@Override
	public int insertBoardFirstOrder(BoardOrder bo) throws SQLException {
			return session.insert("insertBoardOrder", bo);
	}

	@Override
	public List<BoardList> searchBoardList() throws SQLException {
		return session.selectList(makeSqlId("searchBoardList"));
	}
	
	@Override
	public int updateBoardManagementById(BoardManagement bm) throws SQLException {
		return session.update(makeSqlId("updateBoardManagementById"), bm);
	}
	
	@Override
	public int deleteBoardManagementById(int boardId) throws SQLException {
		return session.delete(makeSqlId("deleteBoardManagementById"), boardId);
	}

	@Override
	public int dropBoardPaper(int boardId) throws SQLException {
		return session.update(makeSqlId("dropBoardPaper"), makeTableMap(makeTableName(boardId)));
	}
	
	@Override
	public int dropBoardPaperSequence(int boardId) throws SQLException {
		return session.update(makeSqlId("dropBoardPaperSequence"), makeTableMap(makeSequenceName(boardId)));
	}
	
	@Override
	public List<BoardSort> searchSortList() throws SQLException {
		return session.selectList(makeSqlId("searchSortList"));
	}
	
	@Override
	public List<BoardSort> searchOpenList() throws SQLException {
		return session.selectList(makeSqlId("searchOpenList"));
	}

	@Override
	public int updateBoardSort(int[] sort) throws SQLException {
		for(int no = 0; no < sort.length; no++) {
			Map param = new HashMap();
			param.put("no", no+1);
			param.put("id", sort[no]);
			session.update(makeSqlId("updateBoardSort"), param);
		}
		return 0;
	}

	private String makeTableName(int boardId) {
		return "board_"+boardId+"_paper";
	}
	
	private String makeSequenceName(int boardId) {
		return "board_"+boardId+"_paper_seq";
	}
	
	private Map makeTableMap(String tableName) {
		Map<String, String> parameters = new HashMap<String, String>();
		parameters.put("tableName", tableName);
		return parameters;
	}
	private String makeSqlId(String id){
		return "droowa.boardManagement."+id;
	}
}
